package com.welike.shibo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welike.shibo.eneity.Goods;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.mapper.GoodsMapper;
import com.welike.shibo.service.GoodsService;
import com.welike.shibo.service.ShopHttpService;
import com.welike.shibo.utils.CommonUtil;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by fangyu on 2019-09-10.
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private ShopHttpService shopHttpService;

    /**
     * 通过shop_id 查询商品名
     *
     * @param id 商品id
     * @return 商品名
     */
    @Override
    public String queryNameByShopId(String id) {
        return goodsMapper.queryNameByShopId(id);
    }

    @Override
    public IPage<Goods> queryGoodsPage(int page, int pageSize) {
        Page<Goods> p = new Page<>(page, pageSize);
        return goodsMapper.queryGoodsPage(p);
    }


    @Override
    public JsonResult addGoods(JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "id");
        String id = requestJson.getString("id");
        // shopId 不为空才判断
        if (!Tools.isNull(id)) {
            // 先查数据库是否有此商品， 没有则去查询
            if (goodsMapper.queryNameByShopId(id) == null) {
                // 通过第三方接口查询商品详情
                Goods goods = shopHttpService.getGoodsInfoById(id);
                if (goods != null) {
                    this.save(goods);
                    return ResultUtils.success("goods", goods);
                }
                return ResultUtils.error(ResultEnum.GOODS_NOT_EXIST);
            }
            return ResultUtils.error(ResultEnum.GOODS_EXIST);
        }
        return ResultUtils.error(ResultEnum.PARAM_ERROR);
    }

    @Override
    public JsonResult searchGoods(String good) {
        Goods result;
        // 先查数据库是否有此商品， 没有则去查询
        // 通过商品名称模糊查
        log.info("goods => {}", good.length());
        if (good.length() >= 2){
            result = goodsMapper.queryAllByNameLike(good);
            if (result != null) {
                return ResultUtils.success("goods", result);
            }
        }
        // 通过id查
        result = goodsMapper.queryAllByShopId(good);
        if (result != null) {
            // 通过第三方接口查询商品详情
            return ResultUtils.success("goods", result);
        }else {
            result = shopHttpService.getGoodsInfoById(good);
            if (result != null) {
                this.save(result);
                return ResultUtils.success("goods", result);
            }
            return ResultUtils.error(ResultEnum.GOODS_NOT_EXIST);
        }
    }
}
